<template>
    <div class="todo-title">
        <h1>ToDoList</h1>
        <input type="text" placeholder="请输入任务名称，按回车键确认" v-model="title" @keyup.enter="add"/>
    </div>
</template>

<script>
//nanoid是个函数，进入后直接调用就能给一个唯一的字符串
import {nanoid} from 'nanoid'
export default {
    name:'UserHeader',
    data() {
        return {
            title:''
        }
    },
    methods:{
        add(){
            if(this.title == ''){
                alert('输入不能为空')
            }
            else{
                //将用户的输入包装称为一个TODO对象 
                const todoObj = {id:nanoid(),title:this.title,done:false}
                //通知App组件去添加一个todo对象
                // this.addtodo(todoObj)
                this.$store.commit('ADD_TODOS',todoObj)
                //清空输入
                this.title = ''
            }
        }
    },
    //监视数组todos里的变化，并且要深度监视才能监视到done的变化
    watch:{
        '$store.state.todos':{
            deep:true,
            handler(value){
                localStorage.setItem('todos',JSON.stringify(value))
            }
        }
    }
}
</script>

<style scoped>
.todo-title input:focus {
    outline: none;
    border-color: rgba(237, 163, 199, 0.8);
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(221, 190, 213, 0.6);
}
.todo-title{
    width: 800px;
    height: 50px;
    background-color: #df8ab2;
}
.todo-title h1{
    float: left;
    margin-top:3px;
    margin-left: 30px;
    color: white;
}
.todo-title input{
    float: right;
    margin-right: 30px;
    margin-top: 9px;
    width: 500px;
    height: 25px;
}
</style>